﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_listTrain : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["CurrentPage"] = 1;
            ViewState["PageCount"] = 1;

            #region 加载最新动态
            CmsDataContext db = new CmsDataContext();

            var px2 = (from aA in db.Article where aA.Verified != null orderby aA.Verified descending select aA).Take(6);
            Repeater3.DataSource = px2;
            Repeater3.DataBind();
            #endregion

            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();

        var ts = from aT in db.Train
                 orderby aT.Started descending
                 select new
                 {
                     ID = aT.ID,
                     Memo = string.Format("{0}将于{1:yyyy-MM-dd HH:mm}在{2}举办", aT.Name, aT.Started, aT.Address),
                     Amended = aT.Amended != null,
                     Ended = aT.Started.CompareTo(DateTime.Now) <= 0,
                     District = aT.District
                 };

        pds.DataSource = ts.ToList();
        pds.PageSize = 20;
        pds.AllowPaging = true;

        pds.CurrentPageIndex = (int)ViewState["CurrentPage"] >= pds.PageCount ? pds.PageCount - 1 : (int)ViewState["CurrentPage"] - 1;

        Repeater1.DataSource = pds;
        Repeater1.DataBind();

        ViewState["PageCount"] = pds.PageCount;
    }

    protected void ChangePage(object sender, ImageClickEventArgs e)
    {
        if (sender == FirstButton)
        {
            ViewState["CurrentPage"] = 1;
        }
        else if (sender == PreviousButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["CurrentPage"] == 1 ? 1 : (int)ViewState["CurrentPage"] - 1;
        }
        else if (sender == NextButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["CurrentPage"] == (int)ViewState["PageCount"] ? (int)ViewState["PageCount"] : (int)ViewState["CurrentPage"] + 1;
        }
        else if (sender == LastButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["PageCount"];
        }

        BindData();
    }
}